package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.feign.VodServiceApi;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author Cyrus
 * @since 2020-12-08
 */
@Api(description = "小节服务")
@RestController
@RequestMapping("/eduservice/video")
public class EduVideoController {

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private VodServiceApi vodServiceApi;


    @PostMapping("/addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo) {
        videoService.save(eduVideo);
        return R.ok();
    }

    @GetMapping("/getVideoInfo/{videoId}")
    public R getVideoInfo(@PathVariable String videoId) {
        EduVideo video = videoService.getById(videoId);
        return R.ok().data("video",video);
    }

    @PostMapping("/updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo) {
        videoService.updateById(eduVideo);
        return R.ok();
    }

    @DeleteMapping("/deleteVideo/{videoId}")
    public  R  deleteVideo(@PathVariable String videoId) {
        EduVideo eduVideo = videoService.getById(videoId);
        String id = eduVideo.getVideoSourceId();
        if (!StringUtils.isEmpty(id)){
            R result = vodServiceApi.removeAliyunVideo(id);
            if (result.getCode() == 20001) {
                throw new GuliException(result.getCode(), result.getMessage());
            }
        }
        boolean flag = videoService.removeById(videoId);
        if (flag) {
            return R.ok();
        } else {
            throw new GuliException(20001, "删除小结失败");
        }

    }

}

